package com.tarena.lbs.user.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.dao.mapper.UserTagsMapper;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.po.UserTagsPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepositoryImpl implements UserRepository {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserTagsMapper userTagsMapper;

    @Override
    public UserPO getByPhone(String phone) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Long countUserByPhone(String phone) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("phone",phone);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public void save(UserPO poParam) {
        userMapper.insert(poParam);
    }

    @Override
    public UserPO getById(Integer userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public void add(UserTagsPO userTagsPO) {
        userTagsMapper.insert(userTagsPO);
    }

    @Override
    public void deleteUserTagsByUserId(Integer userId) {
        QueryWrapper<UserTagsPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        userTagsMapper.delete(queryWrapper);
    }

    // 小程序第三方登录相关
    @Override
    public UserPO getOne(QueryWrapper<UserPO> queryWrapper) {
        return userMapper.selectOne(queryWrapper);
    }

    // 更新
    @Override
    public void updateById(UserPO userPO) {
        userMapper.updateById(userPO);
    }
}
